require 'rubygems'
require 'hpricot'
require 'macaddr'
require 'uuidtools'

include ScraperHtml


class Product < ActiveRecord::Base
  belongs_to :category,:counter_cache=>true
  belongs_to :brand
  belongs_to :abstract_product
  has_and_belongs_to_many :properties, :join_table => "holds"
  before_create :create_uuid,:clear_description
  before_update :clear_description

  GEOMETRY_IMAGE_SIZE = "360X360"
  MEDIUM_IMAGE_SIZE = "200X200"
  THUMB_IMAGE_SIZE = "100X100"


  file_column :image_url, :magick => {:geometry=>GEOMETRY_IMAGE_SIZE,
    :versions => { "thumb" => THUMB_IMAGE_SIZE,"medium" => MEDIUM_IMAGE_SIZE}
  }

  def self.scraper(site,category_id,brand_id,object_type)
    result = 0
    for i in site[:start].to_i..site[:end].to_i
      doc = open(site[:url]+i.to_s){ |f| Hpricot(f)}
      iconv=Iconv.new("UTF-8//IGNORE",site[:encoding])
      #从xpath中获取数据

      if !site[:name].blank?
        pname = iconv.iconv(doc.search((site[:name])).inner_text) if
        pname =  pname.gsub(Regexp.new(site[:name_filter]),"") if !site[:name_filter].blank?
      else
        pname = Category.find(category_id).name
      end
      
      
      description = ""
      description = iconv.iconv(doc.search(site[:desc1]).inner_text) if !site[:desc1].blank?
      description += iconv.iconv(doc.search(site[:desc2]).to_html) if !site[:desc2].blank?
      description += iconv.iconv(doc.search(site[:desc3]).to_html) if !site[:desc3].blank?
      description.gsub!(Regexp.new(site[:desc_filter]),site[:desc_replace]) if !site[:desc_filter].blank?
      description.gsub!(Regexp.new(site[:desc_filter1]),site[:desc_replace1]) if !site[:desc_filter1].blank?



      unless site[:image].blank?
        image_tag =  doc.search(site[:image]).to_html
        image_url = /(src=")((.)+?)"/.match(image_tag).values_at(2).to_s
        image_url.gsub!("..","")
        site_root = /http:\/\/.+?\//.match(site[:url]).values_at(0).to_s
        image_url = site_root+image_url if !image_url.include?("http")
      end
      

      if object_type == "p"

        model = iconv.iconv(doc.search(site[:model]).inner_text) if site[:model] && !site[:model].blank?
        model.gsub!(Regexp.new(site[:model_filter]),"") if !site[:model_filter].blank?
        if model && !model.blank?
          product = Product.new(:brand_id => brand_id,
            :category_id=>category_id,
            :model=>model,
            :name=>model + " " + pname,
            :description=>description)
          if product.save
            if image_url
              image_path = save_image(image_url,"product",product.id.to_s)
              sql = ActiveRecord::Base.connection();
              sql.update "UPDATE products SET image_url='#{File.basename(image_path)}' WHERE id=#{product.id}";
            end
            result += 1
          end
        end
      elsif object_type == "c"
        aproduct = AbstractProduct.create()
        if site[:image] && !site[:image].blank?
          image_path = save_image(image_url,"abstract_product",aproduct.id.to_s)
          sql = ActiveRecord::Base.connection();
          sql.update "UPDATE categories SET image_url='#{File.basename(image_path)}' WHERE id=#{aproduct.id}";
        end

        if !site[:horv].blank? && !site[:product_table_start].blank? && !site[:product_table].blank?
              
          if site[:horv] == "v"
            tr = doc.search(site[:product_table]+" tr")
            for loop in site[:product_table_start].to_i..tr.length
              model = doc.search(site[:product_table]+" tr[#{loop}] td[1]").inner_text
              model.gsub!(Regexp.new(site[:model_filter]),"") if !site[:model_filter].blank?
              Product.create(:category_id=>category_id,
                :name=>model + " " + pname,
                :model=>model,
                :abstract_product_id=>aproduct.id,
                :brand_id=>brand_id)
            end
          elsif site[:horv] == "h"
            td = doc.search(site[:product_table]+" tr[1] td")
            for loop in site[:product_table_start].to_i..td.length
              model = doc.search(site[:product_table]+" tr[1] td[#{loop}]").inner_text
              model.gsub!(Regexp.new(site[:model_filter]),"") if !site[:model_filter].blank?
              Product.create(:category_id=>category_id,
                :name=>model + " " + pname,
                :model=>model,
                :abstract_product_id=>aproduct.id,
                :brand_id=>brand_id)
            end
          end
        end

        result +=1

      end
    end
    return result
  end

  def create_uuid
    UUID.mac_address = Mac.addr
    self.uuid = UUID.timestamp_create().to_s
  end

  def self.maintain(new_category_id,old_category_id)
    if new_category_id != old_category_id
      Category.update_counters old_category_id, :products_count=>-1
      Category.update_counters new_category_id, :products_count=>1
    end
  end

  def find_by_brand_and_category(category_id,brand_id)
    find_by_sql(["SELECT DISTINCT p.*
FROM products p, categories c
WHERE  p.category_id = c.id
AND (
c.id = ?
)
and p.brand_id = ?
ORDER BY p.sort_order DESC",category_id,brand_id])
  end

  private
 
  def clear_description
    self.description = clean_tag(self.description) if self.description
  end
end
